package com.ctg.itrdc.imageconsole.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.common.util.TimeUtils;
import com.ctg.itrdc.imageconsole.data.dao.IDockerMicroAppReferDao;
import com.ctg.itrdc.imageconsole.data.entity.DockerMicroAppRefer;
import com.ctg.itrdc.imageconsole.service.IServiceDependService;
import com.ctg.itrdc.pasp.security.utils.WebUtils;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;
import com.ctg.itrdc.paspsdk.common.utils.type.StringUtils;

@Component("serviceDependService")
public class ServiceDependServiceImpl implements IServiceDependService {


	@Resource
	private IDockerMicroAppReferDao appReferDao;

	@Override
	public PageInfo<DockerMicroAppRefer> queryDependencePageInfo(DockerMicroAppRefer appRefer, Integer currentPage,
			Integer perPageNum) {
		appRefer.setCreateUserId(WebUtils.getCtgUser().getSysUserId());
		PageInfo<DockerMicroAppRefer> pageInfo = appReferDao.jdbcFindPageInfo(appRefer, currentPage, perPageNum);

		if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
			for (DockerMicroAppRefer obj : pageInfo.getList()) {
				// TODO 这里是由于前端那边的要求：要求后端接收与返回都是以时间撮的形式。有问题联系前端
				if (StringUtils.isNotEmpty(obj.getCreateTime())) {
					Long createTime = TimeUtils.parse(obj.getCreateTime()).getTime();
					obj.setCreateTime(createTime.toString());
					Long updateTime = TimeUtils.parse(obj.getLastUpdateTime()).getTime();
					obj.setLastUpdateTime(updateTime.toString());
				}
			}
		}
		return pageInfo;
	}


	@Override
	public void saveDependence(DockerMicroAppRefer appRefer) {
		long userId = WebUtils.getCtgUser().getSysUserId();
		if (null == appRefer.getId()) {
			appRefer.setCreateTime(TimeUtils.getNowDateStr());
			appRefer.setLastUpdateTime(TimeUtils.getNowDateStr());
			appRefer.setCreateUserId(userId);
			appReferDao.insert(appRefer);
		}else {
			DockerMicroAppRefer appReferForQuery = new DockerMicroAppRefer();
			appReferForQuery.setId(appRefer.getId());
			appReferForQuery = appReferDao.selectByPrimaryKey(appReferForQuery);
			appReferForQuery.setLastUpdateUserId(userId);
			appReferForQuery.setLastUpdateTime(TimeUtils.getNowDateStr());
			appReferForQuery.setName(appRefer.getName());
			appReferForQuery.setDescJson(appRefer.getDescJson());
			appReferDao.updateByPrimaryKey(appReferForQuery);
		}
	}

	@Override
	public DockerMicroAppRefer queryDependenceDetail(Long dependenceId) {
		DockerMicroAppRefer appRefer = new DockerMicroAppRefer();
		appRefer.setId(dependenceId);
		return appReferDao.selectByPrimaryKey(appRefer);
	}


	@Override
	public void deleteDependence(Long dependenceId) {
		DockerMicroAppRefer appRefer = new DockerMicroAppRefer();
		appRefer.setId(dependenceId);
		appReferDao.deleteByPrimaryKey(appRefer);
	}

	 public static void main(String[] args) {
//		 String json = "[{'name':'ctg-app01181441-web01181443-web-alogic','x':1,'y':1,'children':[ {'name':'ctg-cbs-portal-web','children':[],'y':2,'x':1},{'name':'ctg-m2m-sysmgr-war','children':[],'y':2,'x':3}]}]";
//		 List<ServiceDependDataDTO> serviceDependDataDTOs = JSONUtils.jsonToObjectList(json,ServiceDependDataDTO.class);
	 }
}
